package com.alstudio.net.protocol.xmpp;

import android.os.Handler;

import com.alstudio.net.callback.XmppEventListener;
import com.alstudio.net.protocol.xmpp.data.XmppConnectionState;
import com.alstudio.net.protocol.xmpp.data.XmppData;
import com.alstudio.net.protocol.xmpp.packact.Message;
import com.alstudio.utils.log.Mylog;

public class XmppAdapter implements XmppListener {

	/* XMPP连接控制对象 */
	private XmppProtocol mProtocol = null;
	// XMPP事件监听器
	private XmppEventListener mListener;

	private Handler mHandler;

	public XmppAdapter(XmppEventListener listener) {
		// TODO Auto-generated constructor stub
		mListener = listener;
		mHandler = new Handler();
	}

	/**
	 * 设定回调函数
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void addXmppListener(XmppEventListener listener) {
		this.mListener = listener;
	}

	/**
	 * 移除回调函数
	 * 
	 * @author Alonso Lee
	 */
	public void removeXmppListener() {
		this.mListener = null;
	}

	/**
	 * 设定连接参数
	 * 
	 * @param data
	 * @author Alonso Lee
	 */
	public void setXmppData(XmppData data) {
		if (mProtocol != null) {
			mProtocol.setXmppData(data);
		}
	}

	/**
	 * 获取连接参数
	 * 
	 * @return
	 * @author Alonso Lee
	 */
	public XmppData getXmppData() {
		return mProtocol == null ? mProtocol.getXmppData() : null;
	}

	/**
	 * 登录IM服务器
	 * 
	 * @param data
	 * @author Alonso Lee
	 */
	public void xmppLogin(XmppData data) {
		if (mProtocol != null) {
			if (mProtocol.getConnectionState() != XmppConnectionState.XMPP_STATE_IDLE) {
				// 当前已经连接了，那么先断开当前的连接
				mProtocol.logoff();
			}
		}

		mProtocol = new XmppProtocol(data, this);
		// 开始登录咯
		mProtocol.start();
	}

	/**
	 * 发送反馈
	 */
	public void xmppSendFeedBack() {
		if (isLogined()) {
			mProtocol.sendFeedBack("alonso", "lizhenchang@gmail.com");
		}
	}

	/**
	 * 发送出席消息
	 * 
	 * @author Alonso Lee
	 */
	public boolean xmppSendPresence() {
		Mylog.d("calling xmppSendPresence");

		if ((mProtocol == null) || (!mProtocol.isAuthenticated())) {
			Mylog.w("No XMPP Connection.Refuse");
			return false;
		}
		mProtocol.setStatus(null, null, 50);
		return true;
	}

	/**
	 * 发送消息。支持发送文本或多媒体消息。支持群发。
	 * 
	 * @param msg
	 *            待发送的消息。请确保发送者(from)和接收者(to)不为空，并且为标准的jid。如:"8618676722583@mk"。
	 *            如果是多媒体消息，请 调用setType设置具体的消息类型。
	 * @return true 消息发送成功,false 消息发送失败
	 */
	public boolean xmppSendMessage(Message msg) {
		Mylog.d("calling xmppSendMessage");

		if (msg == null) {
			return false;
		}

		if ((mProtocol == null) || (!mProtocol.isAuthenticated())) {
			Mylog.w("No XMPP Connection.Refuse");
			if (mListener != null) {
				// mListener.onSendMessageFailedEvents(msg.getMsgId());
			}
			return false;
		}
		return mProtocol.sendMessage(msg);

	}

	/**
	 * 发送c2s ping包请求
	 */
	public void xmppSendPingRequest() {

		Mylog.d("calling xmppSendMessage");

		if ((mProtocol == null) || (!mProtocol.isAuthenticated())) {
			Mylog.w("No XMPP Connection.Refuse");
			return;
		}
		mProtocol.sendPingRequest();
	}

	/**
	 * 响应服务器下发的S2C心跳包。此方法不会暴露给上层。
	 * 
	 * @return
	 * @author Alonso Lee
	 */
	private void xmppSendPingResponse(String id, String from, String to) {
		if (isLogined()) {
			mProtocol.sendPingResponse(id, from, to);
		}
	}

	private boolean isLogined() {
		if (mProtocol != null && mProtocol.isAuthenticated()) {
			return true;
		}
		Mylog.w("No XMPP Connection.Refuse");
		return false;
	}

	@Override
	public void onJxaConnFailed(String msg, String conflictSid) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaConnFailed(String msg) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaConnStateChange(XmppConnectionState state) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaAuth(final String username, final String passwrod,
			String giftVersion) {
		// TODO Auto-generated method stub
		mHandler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				mListener.onLoginedEvent(username, passwrod);
				Mylog.e("出席了");
			}
		});
	}

	@Override
	public void onJxaAuthFailed(String message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaLogoutEvent(boolean result) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaChangePasswordEvent(int code, String newPwd) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaStatusEvent(String user, String show) {
		// TODO Auto-generated method stub
		mListener.onPresenceStatusEvent(user, show);
	}

	@Override
	public void onJxaErrorOccur(String code, String nameSpace) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaMessageEvent(final Message msg) {
		// TODO Auto-generated method stub
		mHandler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				mListener.onReceiveMessage(msg);

			}
		});
	}

	@Override
	public void onJxaGetMessageBackEvent(String from, String id) {
		// TODO Auto-generated method stub
		mListener.onReceiveMessageBackEvent(from, id);
	}

	@Override
	public void onJxaRegisterEvent(String jid, String password, String user) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onJxaGetPingEvent(int type, String id, String from, String to) {
		// TODO Auto-generated method stub

		// 此条心跳包的类型。取值范围: "0" 服务器下发的S2C心跳包，程序必须响应。否则服务器将把连接断开
		// 。"1"服务器响应本程序上传的C2S心跳包。证明当前连接正常
		switch (type) {
		case 0:
			mListener.onGetPingPackageEvent(false);
			xmppSendPingResponse(id, from, to);
			Mylog.e("id "+id);
			break;
		case 1:
			mListener.onGetPingPackageEvent(true);
			break;
		}
	}
}
